package com.jxd.ordering.service.impl;

import com.jxd.ordering.dao.DictMapper;
import com.jxd.ordering.service.IDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DictServiceImpl
 * @Description TODO
 * @Author Ankailong
 * @Date 2022/9/25
 * @Version 1.0
 */
@Service
public class DictServiceImpl implements IDictService
{
    @Autowired
    private DictMapper dictMapper;
    private Map<String, Map<String, Map<String, String>>> dict = null;

    /**
     * 获取所有数据字典信息
     * @return 数据字典信息集合
     */
    @Override
    public Map<String, Map<String, Map<String, String>>> getAllDict() {
        List<Map<String, Object>> dictList = dictMapper.selectAllDict();
        Map<String, Map<String, Map<String, String>>> dictMap = new HashMap<>();

        for (Map<String, Object> dict : dictList) {
            //field 字段  name 名称
            if (!dictMap.containsKey(dict.get("field"))) {
                dictMap.put((String) dict.get("field"), new HashMap<>());
            }
            Map<String, String> map = new HashMap<>();
            map.put("text", (String) dict.get("name"));
            map.put("id", dict.get("id").toString());
            map.put("pid", dict.get("pid").toString());
            dictMap.get(dict.get("field")).put(dict.get("value").toString(), map);
        }

        return dictMap;
    }

    /**
     * 根据字段类型和字段值获取数据字典的字段名称
     * @param field 字段类型
     * @param value 字段值
     * @return 字段名称
     */
    @Override
    public String getDict(String field, String value) {
        if (dict == null) {
            dict = getAllDict();
        }
        if (dict.get(field).containsKey(value)) {
            return dict.get(field).get(value).get("text");
        } else {
            return "";
        }

    }
}
